using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Presentation;

namespace IGlobalist.Cms
{
    public partial class RegisterUser : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string url= Request.UrlReferrer.PathAndQuery;
                int totalString = url.LastIndexOf('?');
                if (totalString>1)
                    url = url.Substring(0, totalString);
                #region login
                if (Request["login"] == "1")
                {
                    //login 
                    if (IGlobalistMembership.ValidateUserSite(Convert.ToString(Request["tbUsername"]), Convert.ToString(Request["tbPassword"])))
                    {
                        //success
                        string remember = Request["cbRemember"];

                        HttpCookie memberLogin = new HttpCookie("memberLogin","1");
                        HttpContext.Current.Response.Cookies.Set(memberLogin);
                        string value = string.Format("{0},{1}", Request["tbUsername"], Request["tbPassword"]);
                        if(remember=="1")
                        {
                            HttpCookie rememberuser = new HttpCookie("rememberUser", value);
                            HttpContext.Current.Response.Cookies.Add(rememberuser);
                        }
                        url = string.Format("{0}?success=1", url);
                    }
                    else
                    {
                        //error
                        url = string.Format("{0}?login=2", url);
                    }
                }
                #endregion

                #region new
               
                if (Request["new"]!=null)
                {
                    //new user
                    ICmsClass userClass = new CmsClass();
                    IList<ICmsClass> list = ClassController.GetClassListByClassTypeID((short)CmsObjectType.User);
                    userClass = list[0];

                    IUser user = new User();
                    user.ClassID = userClass.ID;
                    user.Class = userClass;
                    user.ClassTypeID = (short)CmsObjectType.User;                   

                    user.ExtPropertyList = userClass.PropertyList;
                    user.Name = Request["tbUsername"];
                    user.Password = Request["tbPassword"];
                    user.Email = Request["tbEmail"];
                    user.Firstname = Request["tbFirstname"];
                    user.Lastname = Request["tbLastname"];
                    user.Address = Request["tbAddress"];
                    user.City = Request["tbCity"];
                    user.Country = Request["ddlCountry"];
                    user.Zipcode = Request["tbZip"];
                    user.Phone = Request["tbTelephone"];
                    user.Description = Request["tbAnswer"];
                    user.CreatedDate = DateTime.Now;

                    long nID = 0;
                    long rolesListID = 0;
                    IList<ICmsClass> rolesList = ClassController.GetClassListByClassTypeID((short)CmsObjectType.Role);
                    foreach (CmsClass n in rolesList)
                    {
                        nID = n.ID;
                        bool flag = false;
                        IList<IRole> rList = RoleController.GetRoleList(nID);
                        foreach (Role r in rList)
                        {
                            if (r.Name == "Member")
                            {
                                rolesListID = r.ID;
                                flag = true;
                                break;
                            }                            
                        }
                        if (flag)
                            break;
                    }
                    
                    

                    string[] roles ={ Convert.ToString(rolesListID)};
                    user.Roles = roles;

                    if (IsExist(user.Name))
                    {
                        if (IsExistMail(user.Email))
                        {
                            UserController.InsertUser(user);
                            IGlobalistMembership.ValidateUserSite(Convert.ToString(Request["tbUsername"]), Convert.ToString(Request["tbPassword"]));
                            //url = string.Format("{0}?success=1", url);
                            url = string.Format("{0}?success=1", "/0_0_0/default.aspx?");
                        }
                        else
                            url = string.Format("{0}?signup=3", url);
                    }
                    else
                    {
                        url = string.Format("{0}?signup=2", url);
                    }

                }
                #endregion

                #region Update

                    if (Request["edit"] != null)
                    {
                        ICmsClass userClassList = new CmsClass();
                        IList<ICmsClass> list = ClassController.GetClassListByClassTypeID((short)CmsObjectType.User);
                        userClassList = list[0];

                        IUser user = new User();
                        user.ClassID = userClassList.ID;
                        user.Class = userClassList;
                        user.ClassTypeID = (short)CmsObjectType.User;

                        long userID = Convert.ToInt64(Request["prfuserID"]);
                        user = UserController.GetUser(userID);

                        string[] arol = user.Roles;
                            user.ID = userID;
                            user.Roles =arol;
                            //user.ExtPropertyList = userClassList.PropertyList;
                            user.Password = Request["prftbPassword"];
                            user.Firstname = Request["prftbFirstname"];
                            user.Lastname = Request["prftbLastname"];
                            user.Address = Request["prftbAddress"];
                            user.City = Request["prftbCity"];
                            user.Country = Request["prfddlCountry"];
                            user.Zipcode = Request["prftbZip"];
                            user.Phone = Request["prftbTelephone"];
                            user.EditedDate = DateTime.Now;                         
                        
                        UserController.UpdateUser(user);
                        url = string.Format("{0}?MyProfile=1", url);
                    }

                #endregion

                #region Forgetpassword

                    if (Request["forgetpass"] != null)
                    {
                        string name = Request["tbUsername"];
                        string email = Request["tbEmail"];
                        string answer = Request["tbAnswer"];

                        if (!IsExist(name))
                        {
                            if (! IsExistMail(email))
                            {
                                if (IsExistAnswer(answer))
                                {
                                    if(IsExistUserEmail(name,email))
                                    {


                                        //send mail
                                        url = string.Format("{0}?getpass=5", url);
                                    }                                    
                                }
                                else
                                    url = string.Format("{0}?getpass=2", url);
                            }
                            else
                                url = string.Format("{0}?getpass=3", url);
                        }
                        else
                            url = string.Format("{0}?getpass=4", url);
                    }
                #endregion

                Response.Redirect(url);
            }
        }

        protected bool IsExist(string name)
        {
            IList<ICmsClass> list = ClassController.GetClassListByClassTypeID((short)CmsObjectType.User);
            IList<IUser> listUser = UserController.GetUserList(list[0].ID);

            foreach (IUser us in listUser)
            {
                if (name == us.Name) return false;
            }
            return true;
        }

        protected bool IsExistMail(string mail)
        {
            IList<ICmsClass> listuser = ClassController.GetClassListByClassTypeID((short)CmsObjectType.User);
            IList<IUser> listUserID = UserController.GetUserList(listuser[0].ID);

            foreach (IUser use in listUserID)
            {
                if (mail == use.Email)
                    return false;
            }
            return true;
        }

        protected bool IsExistAnswer(string answer)
        {
            IList<ICmsClass> listaswer = ClassController.GetClassListByClassTypeID((short)CmsObjectType.User);
            IList<IUser> listAnswer = UserController.GetUserList(listaswer[0].ID);

            foreach (IUser usu in listAnswer)
            {
                if (answer == usu.Description)
                    return true;
            }
            return false;
        }

        protected bool IsExistUserEmail(string username, string mail)
        {
            IList<ICmsClass> alist = ClassController.GetClassListByClassTypeID((short)CmsObjectType.User);
            IList<IUser> aList = UserController.GetUserList(alist[0].ID);

            foreach (IUser s in aList)
            {
                if (username == s.Name && mail==s.Email)
                    return true;
            }
            return false;
        }
    }
}
